package com.java.util;



import com.alibaba.fastjson.JSON;
import com.java.bean.storeInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: ZhangJin
 * @Date: 2020/9/9 11:05
 */
@Slf4j
public class SerializeUtil implements Serializable {
    public static byte[] serialize(Object object) {
        ObjectOutputStream oos = null;
        ByteArrayOutputStream baos = null;
        try {
            // 序列化
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
            byte[] bytes = baos.toByteArray();
            return bytes;
        } catch (Exception e) {
          log.error("。。。");
        }
        return null;
    }

    /*public List<E> MapToList(Map<E, E> Data){

    }*/

    /* javaBean --> Map */
    public static Map<String, String> convertToMap(Object obj) {
        try {
            if (obj instanceof Map) {
                return (Map)obj;
            }
            Map<String, String> returnMap = BeanUtils.describe(obj);
            returnMap.remove("class");
            return returnMap;
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e1) {
            e1.getMessage();
        }
        return new HashMap();
    }

    public static Object unserialize( byte[] bytes) {
        ByteArrayInputStream bais = null;
        try {
            // 反序列化
            bais = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bais);
            return ois.readObject();
        } catch (Exception e) {

        }
        return null;
    }

    // TODO: 2020/9/10   字符串转化为map
    public static Map<String,String> mapStringToMap(String str){
        str=str.substring(1, str.length()-1);
        String[] strs=str.split(",");
        Map<String,String> map = new HashMap<String, String>();
        for (String string : strs) {
            if (!string.contains("http")) {
                String[] split = string.split("=");
                int length = split.length;
                String key = split[0];
                String value = null;
                if (length >= 2) {
                    value = split[1];
                    map.put(key.trim(), value);
                }else {
                    map.put(key.trim(),value);
                }
            }else{
                String[] split = string.split("=");
                String key = split[0];
                StringBuilder value = new StringBuilder();
                /* 若是Url  因为当中会包含 =   特殊处理*/
                for (int i=1;i<split.length;i++){
                    value.append(split[i]).append("=");
                }
                map.put(key.trim(), value.substring(0,value.lastIndexOf("=")));
            }
        }
        return map;
    }


    public static void main(String[] args) {
        String s ="{Type=蛋糕甜点, numberCommit=1585人评论, distance=1.8607km, consumerByAvg=¥55, LocationOfLatAndLong=120.42138694186188  36.11731438906131, nameOfStore=爵士牛排（永旺购物广场店）, UUid=ad524d2e-e211-4266-a437-3a66ad40e59d, Url=https://www.meituan.com/meishi/41416996/, rateStore=3.7分, Location=市北区合肥路672号永旺购物广场3层}";
        Map<String, String> map1 = SerializeUtil.mapStringToMap(s);
        // TODO: 2020/9/22   map --> javabean
        storeInfo storeInfo = JSON.parseObject(JSON.toJSONString(map1), storeInfo.class);
        String distance = storeInfo.getDistance();
        System.out.println(distance);
//        String[] split = map.get("LocationOfLatAndLong").split("  ");
    }
}
